<template>
  <div class="login">
    <Header title="登录" />
    <div class="img">登录</div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="primary"
          native-type="submit"
          color="#ffc400"
        >
          登录账号
        </van-button>
      </div>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="primary"
          color="#ffc400"
          @click="toRegister"
        >
          没账号?去注册
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { reactive, toRefs } from "@vue/reactivity";
import Header from "../../components/Header.vue";
import { useRouter } from "vue-router";
import { Toast } from "vant";
export default {
  components: {
    Header,
  },
  setup() {
    let router = useRouter();
    let data = reactive({
      username: "",
      password: "",
    });

    //提交按钮
    const onSubmit = (value) => {
      let userInfo = JSON.parse(localStorage.getItem("userInfoList"));
      let login = false;
      userInfo.forEach((item) => {
        if (item["用户名"] === value["用户名"]) {
          if (item["密码"] === value["密码"]) {
            login = true;
            return;
          }
        }
      });
      if (login) {
        Toast("登陆成功");
        localStorage.setItem("isLogin", "1");
        router.push("./home");
      }
      else{
        Toast("账号或密码错误");
      }
    };

    //
    const toRegister = () => {
      router.push("./register");
    };
    return {
      ...toRefs(data),
      onSubmit,
      toRegister,
    };
  },
};
</script>

<style lang="less" scoped>
.login {
  .img {
    width: 200px;
    height: 200px;
    background-color: #ffc400;
    font-size: 80px;
    line-height: 200px;
    text-align: center;
    border-radius: 40px;
    margin: 20px auto;
  }
  .register {
    margin-top: 20px;
  }
}
</style>